import hashlib
import mimetypes
import os
import time
import uuid
from hashlib import md5

from django.conf import settings
from django.core.files.base import ContentFile
from django.shortcuts import render, redirect, HttpResponse
from django.http import JsonResponse
from hot import models
from hot.forms.index import ContentForm
from django import forms
from hot.utils import encry


def list(request):
    if request.method == 'GET':
        content_list = models.Administrator.objects.all()

        return render(request, 'account/user.html',
                      {'contents': content_list},
                      )


def delete(request):
    index_id = int(request.POST.get('index_id'))
    models.Administrator.objects.filter(id=index_id).delete()
    return JsonResponse({"code": '200'})


def delete_all(request):
    lst = request.POST.getlist('selected_ids[]')
    for i in lst:
        models.Administrator.objects.filter(id=i).delete()
    return JsonResponse({"code": '200'})


class UserForm(forms.Form):
    username = forms.CharField(label="用户名")
    password = forms.CharField(label="密码")
    re_password = forms.CharField(label="确认密码")

    def clean_re_password(self):
        password = self.cleaned_data['password']
        re_password = encry.md5_string(self.cleaned_data['re_password'])
        print(password, re_password)

        if password != re_password:
            raise forms.ValidationError('两次密码不一致')
        return re_password

    def clean_password(self):
        password = self.cleaned_data['password']
        return encry.md5_string(password)


def add(request):
    if request.method == 'GET':
        return render(request, 'account/form.html')
    form = UserForm(data=request.POST)
    if not form.is_valid():
        print(form.errors)
        return JsonResponse({'code': '-1'})
    dic = form.cleaned_data
    dic.pop('re_password')
    models.Administrator.objects.create(**dic)
    return JsonResponse({'code': '200'})


class RsetUserForm(forms.Form):
    password = forms.CharField(label="密码")
    new_password = forms.CharField(label="新密码")
    re_password = forms.CharField(label="确认密码")

    def clean_re_password(self):
        password = self.cleaned_data['new_password']
        re_password = encry.md5_string(self.cleaned_data['re_password'])
        print(password, re_password)

        if password != re_password:
            raise forms.ValidationError('两次密码不一致')
        return re_password

    def clean_password(self):
        password = self.cleaned_data['password']
        password = encry.md5_string(password)
        return password

    def clean_new_password(self):
        password = self.cleaned_data['new_password']
        password = encry.md5_string(password)
        return password


def reset(request, id):
    if request.method == 'GET':
        return render(request, 'account/reset.html', {'id': id})
    form = RsetUserForm(data=request.POST)
    now_user = models.Administrator.objects.filter(id=id).first()

    if not form.is_valid():
        print(form.errors)
        return JsonResponse({'code': '-1'})
    dic = form.cleaned_data
    if form.cleaned_data['password'] != now_user.password:
        return JsonResponse({'code': '-1'})
    dic.pop('re_password')
    dic.pop('password')
    models.Administrator.objects.filter(id=id).update(password=dic['new_password'])
    return JsonResponse({'code': '200'})


def edit(request, id):
    user = models.Administrator.objects.filter(id=id)
    if request.method == 'GET':
        return render(request, 'account/edit.html', {'user': user.first()})

    user.update(username=request.POST.get('username'))
    return JsonResponse({'code': '200'})
